package com.tanhua.server.controller;

import com.tanhua.manage.domain.db.User;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    //ResponseEntity:响应内容，spring封装的响应http请求的对象
    @GetMapping("/findByMobile")
    public ResponseEntity findByMobile(String phone){

        User user = userService.findByMobile(phone);
        return ResponseEntity.ok(user);
    }

    @PostMapping("/add")
    /*public ResponseEntity saveUser(@RequestBody Map<String,Object> param){
        String mobile= (String) param.get("mobile");
        String password = (String) param.get("password");
        return  userService.saveUser(mobile,password);
    }*/
    public ResponseEntity saveUser(@RequestBody User user){
        userService.saveUser(user);
        return ResponseEntity.ok(null);//.ok状态码为200
    }
    /**
     * 注册登陆 - 发送验证码
     * @param paramMap
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity sendValidateCode(@RequestBody Map<String,String> paramMap){
        //获取手机号码
        String phone = paramMap.get("phone");
        //调用业务发送验证码
        userService.sendValidateCode(phone);
        //响应结果
        return  ResponseEntity.ok(null);
    }

    /**
     * 登陆第二步： 验证码校验
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> paramMap){
        String phone = paramMap.get("phone");
        String verificationCode = paramMap.get("verificationCode");
        Map<String,Object> result = userService.loginVerification(phone,verificationCode);
        return ResponseEntity.ok(result);
    }
}
